
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <title>invokeGuardedCallback · React源码解析</title>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.0">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-theme-api/theme-api.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="../../styles/website.css">
        
    
        
    
        
    
        
    
        
    
        
    

         
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="commitBeforeMutationLifecycles.html" />
    
    
    <link rel="prev" href="commit-root.html" />
    

<script
  async
  src="https://www.googletagmanager.com/gtag/js?id=UA-49483742-5"
></script>
<script>
  window.dataLayer = window.dataLayer || []
  function gtag() {
    dataLayer.push(arguments)
  }
  gtag('js', new Date())

  gtag('config', 'UA-49483742-5')
</script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    首页
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    基础
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../api/react.html">
            
                <a href="../api/react.html">
            
                    
                    React API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../api/react-element.html">
            
                <a href="../api/react-element.html">
            
                    
                    ReactElement
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../api/react-children.html">
            
                <a href="../api/react-children.html">
            
                    
                    React Children
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../api/react-structure.html">
            
                <a href="../api/react-structure.html">
            
                    
                    React 中的数据结构
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    创建更新
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../update/react-dom-render.html">
            
                <a href="../update/react-dom-render.html">
            
                    
                    ReactDOM.render
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../update/set-state.html">
            
                <a href="../update/set-state.html">
            
                    
                    setState & forceUpdate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../update/expiration-time.html">
            
                <a href="../update/expiration-time.html">
            
                    
                    expirationTime 计算
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    任务调度
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../flow/scheduler-global.html">
            
                <a href="../flow/scheduler-global.html">
            
                    
                    全局变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../flow/schedule-work.html">
            
                <a href="../flow/schedule-work.html">
            
                    
                    scheduleWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../flow/scheduler-pkg.html">
            
                <a href="../flow/scheduler-pkg.html">
            
                    
                    react-scheduler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../flow/perform-work.html">
            
                <a href="../flow/perform-work.html">
            
                    
                    performWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../flow/perform-unit-of-work.html">
            
                <a href="../flow/perform-unit-of-work.html">
            
                    
                    performUnitOfWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../flow/render-root.html">
            
                <a href="../flow/render-root.html">
            
                    
                    renderRoot
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.6.1" data-path="../flow/render-root/throw-exception.html">
            
                <a href="../flow/render-root/throw-exception.html">
            
                    
                    throwException
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../flow/begin-work.html">
            
                <a href="../flow/begin-work.html">
            
                    
                    beginWork
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.7.1" data-path="../flow/begin-work/mount-indeterminate-component.html">
            
                <a href="../flow/begin-work/mount-indeterminate-component.html">
            
                    
                    mountIndeterminateComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.2" data-path="../flow/begin-work/mount-lazy-component.html">
            
                <a href="../flow/begin-work/mount-lazy-component.html">
            
                    
                    mountLazyComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.3" data-path="../flow/begin-work/update-functional-component.html">
            
                <a href="../flow/begin-work/update-functional-component.html">
            
                    
                    updateFunctionalComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.4" data-path="../flow/begin-work/update-class-component.html">
            
                <a href="../flow/begin-work/update-class-component.html">
            
                    
                    updateClassComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.5" data-path="../flow/begin-work/update-host-root.html">
            
                <a href="../flow/begin-work/update-host-root.html">
            
                    
                    updateHostRoot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.6" data-path="../flow/begin-work/update-host-component.html">
            
                <a href="../flow/begin-work/update-host-component.html">
            
                    
                    updateHostComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.7" data-path="../flow/begin-work/update-host-text.html">
            
                <a href="../flow/begin-work/update-host-text.html">
            
                    
                    updateHostText
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.8" data-path="../feature/Suspense-component.md">
            
                <span>
            
                    
                    updateSuspenseComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.9" data-path="../flow/begin-work/update-portal-component.html">
            
                <a href="../flow/begin-work/update-portal-component.html">
            
                    
                    updatePortalComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.10" data-path="../flow/begin-work/update-forward-ref.html">
            
                <a href="../flow/begin-work/update-forward-ref.html">
            
                    
                    updateForwardRef
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.11" data-path="../flow/begin-work/update-fragment.html">
            
                <a href="../flow/begin-work/update-fragment.html">
            
                    
                    updateFragment
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.12" data-path="../flow/begin-work/update-mode.html">
            
                <a href="../flow/begin-work/update-mode.html">
            
                    
                    updateMode
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.13" data-path="../flow/begin-work/update-profiler.html">
            
                <a href="../flow/begin-work/update-profiler.html">
            
                    
                    updateProfiler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.14" data-path="../flow/begin-work/update-context-provider.html">
            
                <a href="../flow/begin-work/update-context-provider.html">
            
                    
                    updateContextProvider
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.15" data-path="../flow/begin-work/update-context-consumer.html">
            
                <a href="../flow/begin-work/update-context-consumer.html">
            
                    
                    updateContextConsumer
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../flow/reconcile-children.html">
            
                <a href="../flow/reconcile-children.html">
            
                    
                    reconcileChildren
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.8.1" data-path="../flow/reconcile-children/single.html">
            
                <a href="../flow/reconcile-children/single.html">
            
                    
                    调和单个节点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8.2" data-path="../flow/reconcile-children/array.html">
            
                <a href="../flow/reconcile-children/array.html">
            
                    
                    调和列表
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    commit 阶段
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="commit-root.html">
            
                <a href="commit-root.html">
            
                    
                    commitRoot
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.2" data-path="invokeGuardedCallback.html">
            
                <a href="invokeGuardedCallback.html">
            
                    
                    invokeGuardedCallback
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="commitBeforeMutationLifecycles.html">
            
                <a href="commitBeforeMutationLifecycles.html">
            
                    
                    commitBeforeMutationLifecycles
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="commit-all-host-effects.html">
            
                <a href="commit-all-host-effects.html">
            
                    
                    commitAllHostEffects
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.4.1" data-path="host-effects/CommitPlacement.html">
            
                <a href="host-effects/CommitPlacement.html">
            
                    
                    commitPlacement
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4.2" data-path="host-effects/CommitWork.html">
            
                <a href="host-effects/CommitWork.html">
            
                    
                    commitWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4.3" data-path="host-effects/CommitDeletion.html">
            
                <a href="host-effects/CommitDeletion.html">
            
                    
                    commitDeletion
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="commit-all-lifecycles.html">
            
                <a href="commit-all-lifecycles.html">
            
                    
                    commitAllLifecycle
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    功能
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../features/context.html">
            
                <a href="../features/context.html">
            
                    
                    context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../features/hydrate.html">
            
                <a href="../features/hydrate.html">
            
                    
                    hydrate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../features/ref.html">
            
                <a href="../features/ref.html">
            
                    
                    ref
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../features/event-injection.html">
            
                <a href="../features/event-injection.html">
            
                    
                    事件系统初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../features/event-bind.html">
            
                <a href="../features/event-bind.html">
            
                    
                    事件绑定
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../features/event-dispatch.html">
            
                <a href="../features/event-dispatch.html">
            
                    
                    事件触发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../features/event-create-event-object.html">
            
                <a href="../features/event-create-event-object.html">
            
                    
                    事件对象生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../features/Suspense-component.html">
            
                <a href="../features/Suspense-component.html">
            
                    
                    Suspense
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../features/suspense.html">
            
                <a href="../features/suspense.html">
            
                    
                    挂起任务的含义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="../features/suspense-lazy.html">
            
                <a href="../features/suspense-lazy.html">
            
                    
                    lazy 组件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <span>
            
                    
                    Hooks
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../hooks/hooks-start.html">
            
                <a href="../hooks/hooks-start.html">
            
                    
                    开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../hooks/hooks-use-state.html">
            
                <a href="../hooks/hooks-use-state.html">
            
                    
                    useState
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../hooks/hooks-use-effect.html">
            
                <a href="../hooks/hooks-use-effect.html">
            
                    
                    useEffect
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../hooks/hooks-others.html">
            
                <a href="../hooks/hooks-others.html">
            
                    
                    其他API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../hooks/hooks-common.html">
            
                <a href="../hooks/hooks-common.html">
            
                    
                    通用API
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <form
            action="https://buttondown.email/api/emails/embed-subscribe/Jokcy"
            method="post"
            target="popupwindow"
            onsubmit="window.open('https://buttondown.email/Jokcy', 'popupwindow')"
            class="buttondown-form"
            >
            <input type="email" name="email" id="bd-email" placeholder="(your@email.com)">
            <input type="hidden" value="1" name="embed"/>
            <button type="submit">订阅我</button>
        </form>
    </li>
</ul>

                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                 
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >invokeGuardedCallback</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="invokeguardedcallback">invokeGuardedCallback</h1><p>&#x5728;&#x5F00;&#x53D1;&#x9636;&#x6BB5; React &#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x6765;&#x5E2E;&#x52A9;&#x6536;&#x96C6;&#x9519;&#x8BEF;&#xFF0C;&#x4E00;&#x5F00;&#x59CB;&#x6211;&#x4E00;&#x76F4;&#x6CA1;&#x770B;&#x5230;&#x5230;&#x5E95;&#x662F;&#x7528;&#x6765;&#x505A;&#x4EC0;&#x4E48;&#x7684;&#xFF0C;&#x540E;&#x6765;&#x6839;&#x636E;&#x6CE8;&#x91CA;&#x67E5;&#x4E86;&#x4EE5;&#x4E0B;&#x8D44;&#x6599;&#x4E4B;&#x540E;&#x4E86;&#x89E3;&#x4E86;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x4F5C;&#x7528;&#xFF0C;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x4E00;&#x4E0B;&#x3002;</p><p>&#x9996;&#x5148;&#x662F;&#x7528;&#x6CD5;&#xFF1A;</p><pre><code class="lang-js"><span class="hljs-keyword">if</span> (__DEV__) {
  invokeGuardedCallback(<span class="hljs-literal">null</span>, commitBeforeMutationLifecycles, <span class="hljs-literal">null</span>)
  <span class="hljs-keyword">if</span> (hasCaughtError()) {
    didError = <span class="hljs-literal">true</span>
    error = clearCaughtError()
  }
} <span class="hljs-keyword">else</span> {
  <span class="hljs-keyword">try</span> {
    commitBeforeMutationLifecycles()
  } <span class="hljs-keyword">catch</span> (e) {
    didError = <span class="hljs-literal">true</span>
    error = e
  }
}</code></pre>
<p>&#x53EA;&#x6709;<code>dev</code>&#x73AF;&#x5883;&#x4F1A;&#x6709;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x8FDB;&#x884C;&#x8C03;&#x7528;&#xFF0C;&#x5BF9;&#x6BD4;&#x4EE5;&#x4E0B;&#xFF0C;&#x5728;&#x6B63;&#x5F0F;&#x73AF;&#x5883;&#x4F7F;&#x7528;&#x7684;&#x662F;<code>try catch</code>&#x3002;&#xFF08;&#x8BF7;&#x65E0;&#x89C6;<code>commitBeforeMutationLifecycles</code>&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#xFF09;</p><p>&#x5148;&#x6765;&#x770B;&#x4E00;&#x4E0B;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x5173;&#x952E;&#x4EE3;&#x7801;&#xFF1A;</p><pre><code class="lang-js"><span class="hljs-keyword">const</span> invokeGuardedCallbackDev = <span class="hljs-function"><span class="hljs-keyword">function</span>&lt;<span class="hljs-title">A</span>, <span class="hljs-title">B</span>, <span class="hljs-title">C</span>, <span class="hljs-title">D</span>, <span class="hljs-title">E</span>, <span class="hljs-title">F</span>, <span class="hljs-title">Context</span>&gt;(<span class="hljs-params">
  name: string | null,
  func: (a: A, b: B, c: C, d: D, e: E, f: F</span>) =&gt; <span class="hljs-title">mixed</span>,
  <span class="hljs-title">context</span>: <span class="hljs-title">Context</span>,
  // &#x9700;&#x8981;&#x4F20;&#x9012;&#x7ED9;&#x56DE;&#x8C03;&#x7684;&#x53C2;&#x6570;
) </span>{
  <span class="hljs-comment">// &#x5982;&#x679C;`document`&#x4E0D;&#x5B58;&#x5728;&#x7684;&#x63D0;&#x9192;</span>
  <span class="hljs-keyword">const</span> evt = <span class="hljs-built_in">document</span>.createEvent(<span class="hljs-string">&apos;Event&apos;</span>)

  <span class="hljs-comment">// &#x8BB0;&#x5F55;&#x4F20;&#x5165;&#x7684;&#x9700;&#x8981;&#x8C03;&#x7528;&#x7684;&#x51FD;&#x6570;&#x662F;&#x5426;&#x6709;&#x9519;&#x8BEF;</span>
  <span class="hljs-keyword">let</span> didError = <span class="hljs-literal">true</span>

  <span class="hljs-keyword">let</span> windowEvent = <span class="hljs-built_in">window</span>.event

  <span class="hljs-comment">// &#x6301;&#x7EED;&#x8DDF;&#x8E2A;`window.event`&#x7684;`descriptor`</span>
  <span class="hljs-comment">// https://github.com/facebook/react/issues/13688</span>
  <span class="hljs-keyword">const</span> windowEventDescriptor = <span class="hljs-built_in">Object</span>.getOwnPropertyDescriptor(<span class="hljs-built_in">window</span>, <span class="hljs-string">&apos;event&apos;</span>)

  <span class="hljs-comment">// &#x83B7;&#x53D6;&#x9700;&#x8981;&#x4F20;&#x9012;&#x7ED9;&#x56DE;&#x8C03;&#x7684;&#x53C2;&#x6570;</span>
  <span class="hljs-keyword">const</span> funcArgs = <span class="hljs-built_in">Array</span>.prototype.slice.call(<span class="hljs-built_in">arguments</span>, <span class="hljs-number">3</span>)
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">callCallback</span>(<span class="hljs-params"></span>) </span>{
    fakeNode.removeEventListener(evtType, callCallback, <span class="hljs-literal">false</span>)
    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> <span class="hljs-built_in">window</span>.event !== <span class="hljs-string">&apos;undefined&apos;</span> &amp;&amp; <span class="hljs-built_in">window</span>.hasOwnProperty(<span class="hljs-string">&apos;event&apos;</span>)) {
      <span class="hljs-built_in">window</span>.event = windowEvent
    }

    func.apply(context, funcArgs)
    didError = <span class="hljs-literal">false</span>
  }

  <span class="hljs-keyword">let</span> error
  <span class="hljs-comment">// Use this to track whether the error event is ever called.</span>
  <span class="hljs-keyword">let</span> didSetError = <span class="hljs-literal">false</span>
  <span class="hljs-keyword">let</span> isCrossOriginError = <span class="hljs-literal">false</span>

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleWindowError</span>(<span class="hljs-params">event</span>) </span>{
    error = event.error
    didSetError = <span class="hljs-literal">true</span>
    <span class="hljs-keyword">if</span> (error === <span class="hljs-literal">null</span> &amp;&amp; event.colno === <span class="hljs-number">0</span> &amp;&amp; event.lineno === <span class="hljs-number">0</span>) {
      isCrossOriginError = <span class="hljs-literal">true</span>
    }
    <span class="hljs-keyword">if</span> (event.defaultPrevented) {
      <span class="hljs-comment">// &#x6709;&#x5176;&#x4ED6;&#x5730;&#x65B9;&#x76D1;&#x542C;&#x4E86;`error`&#x4E8B;&#x4EF6;&#x5E76;`preventDefault`</span>
      <span class="hljs-comment">// &#x8BB0;&#x5F55;&#x5E76;&#x8BA9;&#x540E;&#x7EED;&#x4EE3;&#x7801;&#x51B3;&#x5B9A;&#x662F;&#x5426;&#x8981;&#x62A5;&#x544A;</span>
      <span class="hljs-keyword">if</span> (error != <span class="hljs-literal">null</span> &amp;&amp; <span class="hljs-keyword">typeof</span> error === <span class="hljs-string">&apos;object&apos;</span>) {
        <span class="hljs-keyword">try</span> {
          error._suppressLogging = <span class="hljs-literal">true</span>
        } <span class="hljs-keyword">catch</span> (inner) {
          <span class="hljs-comment">// Ignore.</span>
        }
      }
    }
  }

  <span class="hljs-comment">// Create a fake event type.</span>
  <span class="hljs-keyword">const</span> evtType = <span class="hljs-string">`react-<span class="hljs-subst">${name ? name : &apos;invokeguardedcallback&apos;}</span>`</span>

  <span class="hljs-comment">// Attach our event handlers</span>
  <span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">&apos;error&apos;</span>, handleWindowError)
  fakeNode.addEventListener(evtType, callCallback, <span class="hljs-literal">false</span>)

  <span class="hljs-comment">// Synchronously dispatch our fake event. If the user-provided function</span>
  <span class="hljs-comment">// errors, it will trigger our global error handler.</span>
  evt.initEvent(evtType, <span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>)
  fakeNode.dispatchEvent(evt)

  <span class="hljs-keyword">if</span> (windowEventDescriptor) {
    <span class="hljs-built_in">Object</span>.defineProperty(<span class="hljs-built_in">window</span>, <span class="hljs-string">&apos;event&apos;</span>, windowEventDescriptor)
  }

  <span class="hljs-keyword">if</span> (didError) {
    <span class="hljs-keyword">if</span> (!didSetError) {
      <span class="hljs-comment">// &#x51FD;&#x6570;&#x8C03;&#x7528;&#x786E;&#x5B9E;&#x51FA;&#x9519;&#x4E86;&#xFF0C;&#x4F46;&#x662F;`window.error`&#x6CA1;&#x6709;&#x88AB;&#x89E6;&#x53D1;</span>
      <span class="hljs-comment">// &#x8FDB;&#x884C;&#x63D0;&#x9192;&#xFF0C;&#x4EE3;&#x7801;&#x592A;&#x957F;&#x4E0D;&#x5C55;&#x793A;</span>
    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (isCrossOriginError) {
      error = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(
        <span class="hljs-string">&quot;A cross-origin error was thrown. React doesn&apos;t have access to &quot;</span> +
          <span class="hljs-string">&apos;the actual error object in development. &apos;</span> +
          <span class="hljs-string">&apos;See https://fb.me/react-crossorigin-error for more information.&apos;</span>,
      )
    }
    <span class="hljs-keyword">this</span>.onError(error)
  }

  <span class="hljs-comment">// Remove our event listeners</span>
  <span class="hljs-built_in">window</span>.removeEventListener(<span class="hljs-string">&apos;error&apos;</span>, handleWindowError)
}</code></pre>
<p><em>&#x4EE3;&#x7801;&#x4F1A;&#x6709;&#x70B9;&#x957F;&#xFF0C;&#x4E0D;&#x8981;&#x5728;&#x610F;&#x8FD9;&#x4E9B;&#x7EC6;&#x8282;</em></p><p>&#x9996;&#x5148;&#x8BF4;&#x4E00;&#x4E0B;&#x4E3A;&#x4EC0;&#x4E48;&#x8981;&#x7528;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x6765;&#x6536;&#x96C6;&#x9519;&#x8BEF;&#xFF0C;&#x4E3B;&#x8981;&#x539F;&#x56E0;&#x662F;&#x4E3A;&#x4E86;<strong>&#x9632;&#x6B62;&#x6D4F;&#x89C8;&#x5668; DevTool &#x7684;<code>pause on any exception</code>&#x529F;&#x80FD;&#x5BFC;&#x81F4; React &#x6E32;&#x67D3;&#x56E0;&#x4E3A;&#x51FA;&#x73B0;&#x9519;&#x8BEF;&#x88AB;&#x4E2D;&#x65AD;&#xFF0C;&#x800C;&#x5BFC;&#x81F4;&#x6E32;&#x67D3;&#x4E0D;&#x51FA;&#x5185;&#x5BB9;&#xFF0C;&#x56E0;&#x4E3A; React &#x73B0;&#x5728;&#x662F;&#x53EF;&#x4EE5;&#x6355;&#x83B7;&#x9519;&#x8BEF;&#x5E76;&#x4E14;&#x5728;&#x9519;&#x8BEF;&#x7684;&#x65F6;&#x5019;&#x6E32;&#x67D3;&#x5BF9;&#x5E94;&#x7684; UI &#x7684;&#xFF0C;&#x4F46;&#x662F;&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x8FD9;&#x4E2A;&#x529F;&#x80FD;&#x751A;&#x81F3;&#x53EF;&#x4EE5;&#x8BA9;&#x88AB;<code>try catch</code>&#x6355;&#x83B7;&#x7684;&#x9519;&#x8BEF;&#x4E5F;&#x505C;&#x5728;&#x9519;&#x8BEF;&#x51FA;&#x73B0;&#x7684;&#x5730;&#x65B9;&#xFF0C;&#x5BF9; React &#x5F00;&#x53D1;&#x7684;&#x4F53;&#x9A8C;&#x4E0D;&#x662F;&#x5F88;&#x597D;&#x3002;</strong></p><p>OK&#xFF0C;&#x5176;&#x5B9E;&#x6211;&#x5DF2;&#x7ECF;&#x89E3;&#x91CA;&#x5B8C;&#x4E86;&#x3002;</p><p>&#x597D;&#x5427;&#xFF0C;&#x5176;&#x5B9E;&#x8FD8;&#x6709;&#x4E00;&#x4E9B;&#x53EF;&#x4EE5;&#x8BF4;&#x7684;&#xFF0C;&#x6CE8;&#x610F;<code>isCrossOriginError</code>&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#xFF0C;&#x4ED6;&#x4F1A;&#x5728;&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x88AB;&#x8BBE;&#x7F6E;&#x4E3A;<code>true</code>&#x5462;&#xFF1F;</p><pre><code class="lang-js">error = event.error
didSetError = <span class="hljs-literal">true</span>
<span class="hljs-keyword">if</span> (error === <span class="hljs-literal">null</span> &amp;&amp; event.colno === <span class="hljs-number">0</span> &amp;&amp; event.lineno === <span class="hljs-number">0</span>) {
  isCrossOriginError = <span class="hljs-literal">true</span>
}</code></pre>
<p>&#x8FD9;&#x91CC;&#x9762;&#x7684;&#x903B;&#x8F91;&#x662F;&#x4EC0;&#x4E48;&#x5462;&#xFF1F;&#x6D4F;&#x89C8;&#x5668;&#x5BF9;&#x4E8E;&#x9519;&#x8BEF;&#x6536;&#x96C6;&#x662F;&#x6709;&#x5B89;&#x5168;&#x7B56;&#x7565;&#x7684;&#xFF0C;&#x5BF9;&#x4E8E;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#xFF0C;&#x5982;&#x679C;&#x76D1;&#x542C;&#x7684;&#x811A;&#x672C;&#x6765;&#x81EA;<strong>&#x975E;&#x540C;&#x6E90;&#x7684;&#x5730;&#x65B9;</strong>&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x4F1A;&#x963B;&#x6B62;&#x4ED6;&#x6536;&#x96C6;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#xFF08;&#x5E76;&#x6CA1;&#x6709;&#x9A8C;&#x8BC1;&#xFF0C;&#x5927;&#x5BB6;&#x53EF;&#x4EE5;&#x81EA;&#x5DF1;&#x8BD5;&#x8BD5;&#xFF0C;&#x6709;&#x7A7A;&#x6211;&#x4E5F;&#x4F1A;&#x5199;&#x4E2A; demo&#xFF09;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x4E0B;&#x9762;&#x51E0;&#x4E2A;&#x60C5;&#x51B5;&#x4F1A;&#x8BA9; React &#x51FA;&#x73B0;<code>cross-origin</code>&#x7684;&#x9519;&#x8BEF;&#x63D0;&#x9192;&#xFF1A;</p><ul><li>React &#x6765;&#x81EA; CDN
</li>
<li>&#x6784;&#x5EFA;&#x5DE5;&#x5177;&#x8981;&#x4F7F;&#x7528;<code>source-map</code>&#xFF0C;&#x4F7F;&#x7528;<code>eval</code>&#x6765;&#x6267;&#x884C;&#x4EE3;&#x7801;
</li>
<li>&#x5F02;&#x6B65;&#x6A21;&#x5757;&#x52A0;&#x8F7D;
</li></ul>
<p>&#x5BF9;&#x4E8E;&#x7B2C;&#x4E00;&#x79CD;&#x60C5;&#x51B5;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>&lt;script crossorigin src=&quot;...&quot;&gt;&lt;/script&gt;</code>&#x589E;&#x52A0;<code>crossorigin</code>&#x5C5E;&#x6027;&#x6765;&#x544A;&#x77E5;&#x6D4F;&#x89C8;&#x5668;&#x5B89;&#x5168;&#x6027;&#xFF0C;&#x540C;&#x65F6;&#x8981;&#x4FDD;&#x8BC1; CDN &#x8FD4;&#x56DE;&#x7684;&#x5934;&#x4FE1;&#x606F;&#x6709;<code>Access-Control-Allow-Origin: *</code></p><p>&#x5BF9;&#x4E8E;&#x7B2C;&#x4E8C;&#x79CD;&#x60C5;&#x51B5;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x4F7F;&#x7528;<code>webpack</code>&#xFF0C;&#x4E0D;&#x8981;<code>devtool</code>&#x4E0A;&#x4F7F;&#x7528;&#x6709;<code>eval</code>&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x63A8;&#x8350;&#x4F7F;&#x7528;<code>cheap-module-source-map</code></p><p>&#x5BF9;&#x4E8E;&#x7B2C;&#x4E09;&#x79CD;&#x60C5;&#x51B5;&#xFF0C;&#x5982;&#x679C;&#x4F7F;&#x7528;<code>webpack</code>&#xFF0C;&#x53EF;&#x4EE5;&#x914D;&#x7F6E;<a href="https://webpack.js.org/configuration/output/#output-crossoriginloading" target="_blank"><code>crossOriginLoading</code></a></p><p><a href="https://reactjs.org/docs/cross-origin-errors.html" target="_blank">&#x5B98;&#x65B9;&#x6587;&#x6863;</a></p><p>&#x5269;&#x4E0B;&#x7684;&#x5C31;&#x4E0D;&#x518D;&#x8BE6;&#x7EC6;&#x8BB2;&#x89E3;&#x4E86;&#xFF0C;&#x5927;&#x5BB6;&#x6709;&#x5174;&#x8DA3;&#x81EA;&#x5DF1;&#x7814;&#x7A76;&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#x5427;&#xFF0C;&#x4E3B;&#x8981;&#x5C31;&#x662F;&#x5982;&#x4F55;&#x4E3B;&#x52A8;&#x89E6;&#x53D1;&#x4E8B;&#x4EF6;&#x76F8;&#x5173;&#x7684;&#xFF0C;&#x4EE5;&#x53CA;&#x50CF;&#x8BBE;&#x7F6E;<code>didError</code>&#x548C;<code>didSetError</code>&#x53CC;&#x91CD;&#x9A8C;&#x8BC1;&#x6765;&#x786E;&#x5B9A;&#x662F;&#x5426;<code>error</code>&#x4E8B;&#x4EF6;&#x76D1;&#x542C;&#x88AB;&#x8C03;&#x7528;&#x4E4B;&#x7C7B;&#x7684;&#x4EE3;&#x7801;&#x3002;</p>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
<div id="disqus_thread"></div>
<script>
  /**
   *  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
   *  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/
  /*
var disqus_config = function () {
this.page.url = PAGE_URL;  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};
*/
  ;(function() {
    // DON'T EDIT BELOW THIS LINE
    var d = document,
      s = d.createElement('script')
    s.src = 'https://react-source-code.disqus.com/embed.js'
    s.setAttribute('data-timestamp', +new Date())
    ;(d.head || d.body).appendChild(s)
  })()
</script>
<noscript
  >Please enable JavaScript to view the
  <a href="https://disqus.com/?ref_noscript"
    >comments powered by Disqus.</a
  ></noscript
>

            </div>

            

        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"invokeGuardedCallback","level":"1.5.2","depth":2,"next":{"title":"commitBeforeMutationLifecycles","level":"1.5.3","depth":2,"path":"book/commit-phase/commitBeforeMutationLifecycles.md","ref":"book/commit-phase/commitBeforeMutationLifecycles.md","articles":[]},"previous":{"title":"commitRoot","level":"1.5.1","depth":2,"path":"book/commit-phase/commit-root.md","ref":"book/commit-phase/commit-root.md","articles":[]},"dir":"ltr"},"config":{"plugins":["theme-api"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"sharing":{"facebook":false,"twitter":false,"google":false,"weibo":true,"instapaper":false,"vk":false,"all":["weibo","google","twitter","weibo","instapaper"]},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-api":{"languages":[],"split":true,"theme":"light"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"React源码解析","language":"zh-hans","gitbook":"*"},"file":{"path":"book/commit-phase/invokeGuardedCallback.md","mtime":"2019-01-07T03:56:29.823Z","type":"markdown"},"gitbook":{"version":"3.2.0","time":"2019-07-08T12:28:32.264Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-theme-api/theme-api.js"></script>
        
    

    </body>
</html>

